package com.sise.bl.service.impl;

import com.sise.bl.dao.AddressDao;
import com.sise.bl.dao.CartDao;
import com.sise.bl.dao.OrdersDao;
import com.sise.bl.dao.UserDao;
import com.sise.bl.entity.*;
import com.sise.bl.service.OrdersService;
import com.sise.bl.utils.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

@Transactional
@Service("ordersService")
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    @Qualifier("ordersDao")
    private OrdersDao ordersDao;

    @Autowired
    @Qualifier("userDao")
    private UserDao userDao;

    @Autowired()
    @Qualifier("addressDao")
    private AddressDao addressDao;

    @Autowired
    @Qualifier("cartDao")
    private CartDao cartDao;

    @Override
    public void createOrder(String aid, String uid, String sum) {
        User user = userDao.selectUserById(Integer.valueOf(uid));
        Address address = addressDao.selectByAid(Integer.valueOf(aid));
        //1.创建一个订单对象进行保存
        Orders orders = new Orders();

        BigDecimal bsum = new BigDecimal(sum);

        String orderId = RandomUtils.createOrderId();
        orders.setOid(orderId);
        orders.setUser(user);
        orders.setAddress(address);
        orders.setOtime(new Date());
        orders.setOcount(bsum);
        orders.setOstate(1); //未支付状态

        Set<Orders> list = new HashSet<Orders>();
        list.add(orders);

        user.setOrders(list);
        address.setOrders(list);
        Set<Item> items = new HashSet<Item>();
        List<Cart> carts = cartDao.selectCartsByUid(Integer.valueOf(uid));
        for (int i = 0; i < carts.size(); i++) {
            Cart cart = carts.get(i);
            Item item = new Item();
            item.setInum(cart.getCnum());
            item.setIcount(cart.getCcount());
            item.setProduct(cart.getProduct());
            item.setOrders(orders);
            items.add(item);
        }
        orders.setItems(items);
        ordersDao.insertOrders(orders);

        for (Item item : items) {
            ordersDao.insertItem(item);
        }

        cartDao.deleteCartByUid(uid);

    }

    @Override
    public List<Orders> findOrdersByUid(int uid) {
        return ordersDao.selectOrdersByUid(uid);
    }

    @Override
    public Orders findOrderByOid(String oid) {
        //订单和地址
        Orders orders = ordersDao.selectOrdersByOid(oid);
        //2.oid对应的对应的订单项和商品信息
        //订单项和商品信息

        List<Item> list = ordersDao.selectItemByOid(oid);
        Set<Item> items = new HashSet<Item>(list);

        //3.订单项集合设置给订单对象
        orders.setItems(items);
        return orders;
    }

    @Override
    public void updateStateByOid(String oid) {
        Orders orders = ordersDao.selectOrdersByOid(oid);
        orders.setOstate(2);
        ordersDao.updateStateByOid(orders);
    }

    @Override
    public List<Orders> queryAll() {
        return ordersDao.selectAll();
    }
}
